from usercount.models import UserCount


class GenerateUserData(object):

    def get_old_user_list(self,search_date):
        old_user_list = []
        all_before_today_username = UserCount.objects.filter(create_time__lte=search_date).values('username').distinct()
        if all_before_today_username:
            for i in all_before_today_username:
                old_user_list.append(i['username'])
        return old_user_list

    def get_avg_time(self,stay_time,goods_visit_count):
        if stay_time and int(goods_visit_count) != 0:
            # 计算平均停留时长  时长/商品总浏览量
            avg_time = round(stay_time / goods_visit_count)
            # 返回正确的时间格式
            m, s = divmod(avg_time, 60)
            h, m = divmod(m, 60)
            year_avg_time_result = ("%02d:%02d:%02d" % (h, m, s))
        else:
            year_avg_time_result = ("%02d:%02d:%02d" % (0, 0, 0))
        return year_avg_time_result